//获取数据库驱动
const mongoose = require("mongoose");
const {cinemasModel}=require("../db/model/cinemas");
const get = async ({ curPage, eachPage }) => {
    (curPage = ~~curPage), (eachPage = ~~eachPage);
    const data = {
        total: 0,
        maxPage: 0,
        rows: [],
    };
//    console.log(cinemasModel);
    // const cinemasModel = mongoose.model("cinemas");
    //获取当前model映射的集合中的文档总数
    data.total = await cinemasModel.countDocuments();
    //获取总页数
    data.maxPage = Math.ceil(data.total / eachPage);
    const skipNum = (curPage - 1) * eachPage;
    // 获取页面渲染数据
    data.rows = await cinemasModel.find().skip(skipNum).limit(eachPage).sort({_id:-1}).exec();
    // console.log(data);
    return data
};
const findOne = async _id => {
    const cinemasModel = mongoose.model("cinemas");
    // 查找的那条影院
    return await cinemasModel.findOne({ _id });
}
// // query(查询条件),params(修改的属性)
const updata=async(query,params)=>{
    const cinemasModel = mongoose.model("cinemas");
   return await cinemasModel.updateOne(query,params)
}
const add=async ({name,address,phone,status})=>{
      const cinemasModel = mongoose.model("cinemas");
    //   console.log("添加时",{name,age,gender,address,phone,zip,gradle,email},"end");
      return await cinemasModel.create({name,address,phone,status});
}
const remove=async ({_id})=>{
    const cinemasModel = mongoose.model("cinemas");
    // console.log({_id});
  //   console.log("添加时",{name,age,gender,address,phone,zip,gradle,email},"end");
     await cinemasModel.remove({_id});
}
const change = async (newData) => {
    const cinemasModel = mongoose.model("cinemas");
    await cinemasModel.updateOne({ _id: newData._id }, newData);
}
// 获取所有影院 排片用的
const cinemasListAll = async () => await mongoose.model("cinemas").find();
// 通过id找影院  排片用的
const getOne = async _id => {
    const cinemasModel = mongoose.model("cinemas");
    return await cinemasModel.findById({ _id })
  }

  //查询、排片、放映厅获取影院的数据

const getData = async () => {
    const cinemasModel = mongoose.model("cinemas");
    return await cinemasModel.find()
}

module.exports = { get, add, remove,getData ,cinemasListAll,getOne,change,findOne,updata};




